<?php
	/**
	 * @package AmpleHarvest
	 */
	require_once 'TableHeader.php';

	require_once "cTABLE_Drop_Down.php";

	/**
	 * @brief This class Describes the Query_History Table in the AmpleHarvest Database
	 *
	 * - Revision History
	 *  - 0.1.1
	 * 	 - <b>Date</b>: 12 APR 2011
	 * 	 - <b>Author</b>: Randy Blancett
	 * 	  - Added conversion methods to / from Value
	 *  - 0.1
	 * 	 - <b>Date</b>: 30 MAR 2011
	 * 	 - <b>Author</b>: Randy Blancett
	 * 	  - Initial Version
	 *
	 * @author Randy Blancett
	 * @version 0.1.1
	 * @ingroup code classes table
	 */
	class cTABLE_Drop_Down_Options extends cTable
	{
		/**
		 * @{
		 * @name Columns
		 */
		public $m_int_COL_ID = null;
		public $m_int_COL_DropDown_ID = null;
		public $m_int_COL_Order = null;
		public $m_int_COL_Name = null;
		public $m_int_COL_Value = null;
		public $m_int_COL_Lable = null;
		/**
		 * @}
		 */

		/**
		 * This constructor sets all the required column data
		 */
		public function __construct()
		{
			$this->m_str_TableName = "drop_down_options";
			$this->set_Engine(self::C_STR_ENGINE_INNODB);

			//Create Column Objects
			$obj_COL_ID = new cColVarChar("id");
			$obj_COL_ID->set_Length(36);
			$obj_COL_ID->set_Index(abs_Column::c_int_INDEX_PRIMARY);
			$obj_COL_ID->set_Comment("This column holds the ID of the Drop Down Option.");
			$obj_COL_ID->set_Editable(false);
			$obj_COL_ID->set_Label("ID");

			$obj_COL_DropDown_ID = new cColVarChar("dropDownID");
			$obj_COL_DropDown_ID->set_Null(false);
			$obj_COL_DropDown_ID->set_Length(36);
			$obj_COL_DropDown_ID->set_Comment("This column holds the ID of the Drop Down that this Option Belongs to.");
			$obj_COL_DropDown_ID->set_Editable(false);
			$obj_COL_DropDown_ID->set_Label("Drop Down ID");

			$obj_COL_Order = new cColInt("order");
			$obj_COL_Order->set_Comment("This column holds order in which the item should be displayed in the drop down 0 = top.");
			$obj_COL_Order->set_Label("Order");

			$obj_COL_Name = new cColVarChar("name");
			$obj_COL_Name->set_Length(50);
			$obj_COL_Name->set_Comment("This column holds the name of the item in the drop down.");
			$obj_COL_Name->set_Label("Name");

			$obj_COL_Value = new cColVarChar("value");
			$obj_COL_Value->set_Length(20);
			$obj_COL_Value->set_Null(true);
			$obj_COL_Value->set_Comment("This column holds the Value assigned to the Drop down Item.");
			$obj_COL_Value->set_Label("Value");

			$obj_COL_Label = new cColVarChar("label");
			$obj_COL_Label->set_Null(false);
			$obj_COL_Label->set_Length(20);
			$obj_COL_Label->set_Comment("This column holds the Label Assigned to the drop down Item.");
			$obj_COL_Label->set_Label("Label");

			//Set up connection
			$this->m_obj_Connection = new cConnection("AmpleHarvest", "admin");

			//Activate all Debug options
			$this->m_obj_Connection->set_DebugLevel(cLog_Connection_Debug::c_int_LEVEL_HIDDEN);
			$this->m_obj_Connection->set_LogLevel(cLog_Connection::c_int_LEVEL_BASIC);
			//$this->m_obj_Connection->activate_Logging();
			//$this->m_obj_Connection->activate_LogDebug();
			//$this->m_obj_Connection->activate_ScreenDebug();

			$this->m_int_COL_ID = $this->add_Column($obj_COL_ID);
			$this->m_int_COL_DropDown_ID = $this->add_Column($obj_COL_DropDown_ID);
			$this->m_int_COL_Lable = $this->add_Column($obj_COL_Label);
			$this->m_int_COL_Name = $this->add_Column($obj_COL_Name);
			$this->m_int_COL_Order = $this->add_Column($obj_COL_Order);
			$this->m_int_COL_Value = $this->add_Column($obj_COL_Value);
		}

		/**
		 * @deprecated
		 */
		public function insert_DetaultData()
		{
			return $this->insert_DefaultData();
		}

		public function insert_DefaultData()
		{
			//Scheculde Query Data
			$this->set_id("00E0B669-FCAE-421F-B5A8-78CE8E7C6CC4");
			$this->set_dropDownID(cTABLE_Drop_Down::C_STR_DROPDOWN_QUERY_SCHEDULE);
			$this->set_name("None");
			$this->set_order(0);
			$this->set_value("- - - -");
			$this->set_label("None");

			$this->save_Data2DB();

			$this->set_id("6C35AD8F-A8F1-41A7-A2B4-7FD5EA23A9DA");
			$this->set_dropDownID(cTABLE_Drop_Down::C_STR_DROPDOWN_QUERY_SCHEDULE);
			$this->set_name("Daily Query");
			$this->set_order(1);
			$this->set_value("5 * * *");
			$this->set_label("Daily");

			$this->save_Data2DB();

			$this->set_id("0BA84C70-2FAD-49D0-9DBC-490BB82595A8");
			$this->set_dropDownID(cTABLE_Drop_Down::C_STR_DROPDOWN_QUERY_SCHEDULE);
			$this->set_name("Weekly Query");
			$this->set_order(2);
			$this->set_value("4 * 1 *");
			$this->set_label("Weekly");

			$this->save_Data2DB();

			$this->set_id("E6D90C73-A77C-4F19-8F04-CDBF20991A88");
			$this->set_dropDownID(cTABLE_Drop_Down::C_STR_DROPDOWN_QUERY_SCHEDULE);
			$this->set_name("Monthly Query");
			$this->set_order(3);
			$this->set_value("3 1 * *");
			$this->set_label("Monthly");

			$this->save_Data2DB();

			//Standard Sort Options
			$this->set_id("26450EDD-9328-4811-A58F-839C57B24141");
			$this->set_dropDownID(cTABLE_Drop_Down::C_STR_DROPDOWN_SORT_OPTIONS);
			$this->set_name("Score");
			$this->set_order(0);
			$this->set_value("score");
			$this->set_label("Score");

			$this->save_Data2DB();

			$this->set_id("CFEBF1D5-6422-49EA-8CDF-E885C5A6412E");
			$this->set_dropDownID(cTABLE_Drop_Down::C_STR_DROPDOWN_SORT_OPTIONS);
			$this->set_name("Index Date");
			$this->set_order(1);
			$this->set_value("index_date");
			$this->set_label("Index Date");

			$this->save_Data2DB();

			$this->set_id("C053353C-0C43-4C6F-81C1-23889F02541E");
			$this->set_dropDownID(cTABLE_Drop_Down::C_STR_DROPDOWN_SORT_OPTIONS);
			$this->set_name("Harvest Date");
			$this->set_order(2);
			$this->set_value("harvest_date");
			$this->set_label("Harvest Date");

			$this->save_Data2DB();

			$this->set_id("21E6DD06-31FD-4F86-8154-EEAACD7AD4FA");
			$this->set_dropDownID(cTABLE_Drop_Down::C_STR_DROPDOWN_SORT_OPTIONS);
			$this->set_name("Publish Date");
			$this->set_order(3);
			$this->set_value("publish_date");
			$this->set_label("Publish Date");

			$this->save_Data2DB();

			//Standard Sort Direction
			$this->set_id("C57689E5-66E6-4FFC-959C-D53287CB4580");
			$this->set_dropDownID(cTABLE_Drop_Down::C_STR_DROPDOWN_SORT_DIRECTION);
			$this->set_name("Ascending");
			$this->set_order(0);
			$this->set_value("asc");
			$this->set_label("Ascending");

			$this->save_Data2DB();

			$this->set_id("E6E0F9EB-8950-4BA8-88E8-5E1B3B5EA9AC");
			$this->set_dropDownID(cTABLE_Drop_Down::C_STR_DROPDOWN_SORT_DIRECTION);
			$this->set_name("Descending");
			$this->set_order(1);
			$this->set_value("desc");
			$this->set_label("Descending");

			$this->save_Data2DB();

			//Harvest Request Status Options

			$this->set_id("7DE343C6-C22D-4A28-8554-54393EC26D35");
			$this->set_dropDownID(cTABLE_Drop_Down::C_STR_DROPDOWN_HR_STATUS);
			$this->set_name("New");
			$this->set_order(0);
			$this->set_value("0");
			$this->set_label("New");

			$this->save_Data2DB();

			$this->set_id("E9DBF350-848E-43BD-8169-654C37FEEB17");
			$this->set_dropDownID(cTABLE_Drop_Down::C_STR_DROPDOWN_HR_STATUS);
			$this->set_name("Received");
			$this->set_order(1);
			$this->set_value("1");
			$this->set_label("Received");

			$this->save_Data2DB();

			$this->set_id("AD75092F-FE70-4FDD-97FD-2212B831D115");
			$this->set_dropDownID(cTABLE_Drop_Down::C_STR_DROPDOWN_HR_STATUS);
			$this->set_name("Evaluating");
			$this->set_order(2);
			$this->set_value("2");
			$this->set_label("Evaluating");

			$this->save_Data2DB();

			$this->set_id("B6285EBB-B5E4-447D-8BA3-15CCB8C5381A");
			$this->set_dropDownID(cTABLE_Drop_Down::C_STR_DROPDOWN_HR_STATUS);
			$this->set_name("Rejected");
			$this->set_order(3);
			$this->set_value("3");
			$this->set_label("Rejected");

			$this->save_Data2DB();

			$this->set_id("DDE5D466-E2D2-402B-A261-6E0FF35C5214");
			$this->set_dropDownID(cTABLE_Drop_Down::C_STR_DROPDOWN_HR_STATUS);
			$this->set_name("Staging");
			$this->set_order(4);
			$this->set_value("4");
			$this->set_label("Staging");

			$this->save_Data2DB();

			$this->set_id("EADB805D-B468-4631-A6FA-C3EABC79C5D3");
			$this->set_dropDownID(cTABLE_Drop_Down::C_STR_DROPDOWN_HR_STATUS);
			$this->set_name("In Progress");
			$this->set_order(5);
			$this->set_value("5");
			$this->set_label("In Progress");

			$this->save_Data2DB();

			$this->set_id("CCF0316A-30DE-4863-9931-C35D114F0AEB");
			$this->set_dropDownID(cTABLE_Drop_Down::C_STR_DROPDOWN_HR_STATUS);
			$this->set_name("Active");
			$this->set_order(6);
			$this->set_value("6");
			$this->set_label("Active");

			$this->save_Data2DB();

			$this->set_id("34F1F98F-3E51-4F2E-B209-83D520A2D0F7");
			$this->set_dropDownID(cTABLE_Drop_Down::C_STR_DROPDOWN_HR_STATUS);
			$this->set_name("Inactive");
			$this->set_order(7);
			$this->set_value("7");
			$this->set_label("Inactive");

			$this->save_Data2DB();

			$this->set_id("06E7B59D-D60A-4334-B6A1-524985596C36");
			$this->set_dropDownID(cTABLE_Drop_Down::C_STR_DROPDOWN_HR_TYPE);
			$this->set_name("Web Page");
			$this->set_order(0);
			$this->set_value("0");
			$this->set_label("Web Page");

			$this->save_Data2DB();

			$this->set_id("B84B3E35-49E2-494F-81F1-5CA4B243C499");
			$this->set_dropDownID(cTABLE_Drop_Down::C_STR_DROPDOWN_HR_TYPE);
			$this->set_name("Message Board");
			$this->set_order(1);
			$this->set_value("1");
			$this->set_label("Message Board");

			$this->save_Data2DB();

			$this->set_id("8928122F-A6F2-40E3-BE03-E30C3106BC79");
			$this->set_dropDownID(cTABLE_Drop_Down::C_STR_DROPDOWN_HR_TYPE);
			$this->set_name("RSS Feed");
			$this->set_order(2);
			$this->set_value("2");
			$this->set_label("RSS Feed");

			$this->save_Data2DB();

			$this->set_id("8832D072-3717-4EE7-90CB-178C638D3881");
			$this->set_dropDownID(cTABLE_Drop_Down::C_STR_DROPDOWN_HR_FREQUENCY);
			$this->set_name("Once");
			$this->set_order(0);
			$this->set_value("0");
			$this->set_label("Once");

			$this->save_Data2DB();

			$this->set_id("DB919E86-E1F0-42CD-B191-6CC2E2B7A274");
			$this->set_dropDownID(cTABLE_Drop_Down::C_STR_DROPDOWN_HR_FREQUENCY);
			$this->set_name("Daily");
			$this->set_order(1);
			$this->set_value("1");
			$this->set_label("Daily");

			$this->save_Data2DB();

			$this->set_id("5CAE7488-C062-4CA6-81CD-7D090F838591");
			$this->set_dropDownID(cTABLE_Drop_Down::C_STR_DROPDOWN_HR_FREQUENCY);
			$this->set_name("Weekly");
			$this->set_order(2);
			$this->set_value("2");
			$this->set_label("Weekly");

			$this->save_Data2DB();

			$this->set_id("1B6530DF-AC91-4BA3-9FB3-9ED1DC3F5C9A");
			$this->set_dropDownID(cTABLE_Drop_Down::C_STR_DROPDOWN_HR_FREQUENCY);
			$this->set_name("Monthly");
			$this->set_order(3);
			$this->set_value("3");
			$this->set_label("Monthly");

			$this->save_Data2DB();

			$this->set_id("818B7FB4-EB49-4C42-8DB3-6B38B15A8E7A");
			$this->set_dropDownID(cTABLE_Drop_Down::C_STR_DROPDOWN_DEFAULT_YESNO);
			$this->set_name("Yes");
			$this->set_order(0);
			$this->set_value("1");
			$this->set_label("Yes");

			$this->save_Data2DB();

			$this->set_id("16A4F43C-EC61-4803-9148-F9197F10ABE1");
			$this->set_dropDownID(cTABLE_Drop_Down::C_STR_DROPDOWN_DEFAULT_YESNO);
			$this->set_name("No");
			$this->set_order(1);
			$this->set_value("0");
			$this->set_label("No");

			$this->save_Data2DB();

			$this->set_id("981D8255-8CE4-4D03-B4FE-8941736C5F5D");
			$this->set_dropDownID(cTABLE_Drop_Down::C_STR_DROPDOWN_SEARCH_LAYOUT);
			$this->set_name("Standard");
			$this->set_order(0);
			$this->set_value("standard");
			$this->set_label("Standard");

			$this->save_Data2DB();

			return true;
		}

		/**
		 * Returns if the id is a valid and existing ID
		 * @param string $str_User_ID ID of the Page to check (GUID)
		 * @return boolean true if valid false if not
		 */
		public function validate_ID($str_ID)
		{
			// ensure that there is an ID passed and that it is a valid GUID
			if (!empty($str_ID) && preg_match(self::c_REG_GUID_VALID, $str_ID))
			{
				$this->clear_Query();
				$this->add_ColumnQuery($this->m_int_COL_ID, $str_ID);
				if ($this->do_Search() > 0)
				{
					return true;
				}
			}
			return false;
		}

		/**
		 * Saves Information to the Database
		 */
		public function save_Data2DB()
		{
			if ($this->validate_ID($this->get_id()))
			{
				return $this->update_Data2DB();
			}
			else
			{
				return $this->insert_Data2DB();
			}

			return false;
		}
		/**
		 * This function is used to update data that is already in the database
		 */
		public function update_Data2DB()
		{
			//			$this->clear_Update();
			//			$this->add_ColumnQuery($this->m_int_COL_ID, $this->get_id());

			return parent::update_Data2DB();
		}
		/**
		 * This function is used to insert Data into the database
		 */
		public function insert_Data2DB()
		{
			$str_ID = $this->get_id();
			if (empty($str_ID) || (!preg_match(self::c_REG_GUID_VALID, $str_ID)))
			{
				$this->set_id($this->create_GUID());
			}
			//			$this->set_date(date_format(new datetime(), "Y-m-d H:i:s"));
			return parent::insert_Data2DB();
		}
		/**
		 * converts a Value into the label text
		 * @param string $str_DropDown Drop Down ID (GUID)
		 * @param string $str_Value Value to look up
		 * @return string The label if value was found
		 * @since 0.1.1
		 */
		public function convert_Value2Label($str_DropDown, $str_Value)
		{
			$str_Value = trim($str_Value);
			if (!empty($str_DropDown) && preg_match(self::c_REG_GUID_VALID, $str_DropDown) && isset($str_Value))
			{
				$this->clear_Query();
				$this->add_ColumnQuery($this->m_int_COL_DropDown_ID, $str_DropDown);
				$this->add_ColumnQuery($this->m_int_COL_Value, $str_Value);

				if ($this->do_Search() > 0)
				{
					foreach ($this->get_ResultSet() as $arr_Result)
					{
						$this->clear();
						$this->load_DataFromArray($arr_Result);
						return $this->get_label();
					}
				}
			}
			return "";
		}
		
		public function getAllValuesForType($str_DropDown) {
			if (!empty($str_DropDown) && preg_match(self::c_REG_GUID_VALID, $str_DropDown)) {
				$this->clear_Query();
				$this->add_ColumnQuery($this->m_int_COL_DropDown_ID, $str_DropDown);
			
				if ($this->do_Search() > 0) {
					return $this->get_ResultSet();
				}
			}
			return array();
		}
	}
?>
